package com.zhangtong.excel.tool;

import com.zhangtong.excel.entity.Title;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTool {

    public static void fitColumns(XSSFSheet sheet, Title title) {
        for (int i = 0; i < title.size()+1; i++) {
            sheet.autoSizeColumn(i);
        }
    }

    public static CellStyle createTitleStyle(XSSFWorkbook workbook) {
        CellStyle style = createDefaultCellStyle(workbook);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        XSSFFont font = workbook.createFont();
        font.setFontName("simsun");
        font.setFontHeight(16);
        font.setBold(true);
        style.setFont(font);

        return style;
    }

    public static CellStyle createDefaultCellStyle(XSSFWorkbook workbook) {
        CellStyle style = workbook.createCellStyle();

        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        XSSFFont font = workbook.createFont();
        font.setFontName("simsun");
        font.setFontHeight(12);
        style.setFont(font);

        return style;
    }

    public static Cell createCell(Row row, int cellIdx, CellStyle style) {
        Cell cell = row.createCell(cellIdx);
        cell.setCellStyle(style);

        return cell;
    }


}
